package cate.game.role.bag.spice;

import cate.common.table.d.GDBag;
import cate.common.table.d.GDObj;
import cate.common.table.item.IBagItemTpl;
import cate.common.table.item.spice.row.SpiceBaseRow;
import cate.game.role.Role;
import cate.game.role.bag.BagContainer;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.spice.msg.SpiceAddResp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;


public class SpiceBag extends BagContainer<Spice> {
    public SpiceBag(){

    }

    public SpiceBag(Role role){
        super(role);
    }

    @Override
    protected byte getItemType() {
        return GDObj.Type.SPICE;
    }

    @Override
    public int calcCapacity() {
        return GDBag.Capcity.SPICE;
    }

    @Override
    protected SpiceAddResp createItemAddMsg() {
        return new SpiceAddResp();
    }

    @Override
    protected Spice createItem(int tid, long num) {
        return new Spice(role, tid, num);
    }

    @Override
    @JsonIgnore
    public IBagItemTpl getItemTpl(int templateId) {
        return role.getGame().table.spice.base.get(templateId);
    }

    @Override
    public void onEnter() {
        for (Spice spice : getList()) {
            if (StringUtils.isNotBlank(spice.onHeroUid)) {
                Hero hero = role.getBag().hero.getItem(spice.onHeroUid);
                if (hero == null || hero.spicesOn == null || hero.spicesOn.spiceMap.isEmpty() || !hero.spicesOn.spiceMap.containsValue(spice.uid)) {
                    spice.onHeroUid = null;
                }
            }
        }
    }

    @Override
    protected void onItemCreated(Spice spice) {
        SpiceBaseRow tpl = spice.getTpl(role.getGame());
        if (tpl == null){
            return;
        }
        for (int i = 1; i <= tpl.quality; ++i) {
            role.getHistory().action.spiceGetQuality(i,1);
        }
        role.getHistory().action.spiceGetLevel(spice.level, tpl.quality);
    }
}
